package com.phimpme.phimpme;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends ActionBarActivity {

	public static void assertNotNullOrEmpty(Object o) {
		if (!BuildConfig.DEBUG)
			return;
		if (o == null || (o instanceof String && ((String) o).isEmpty()))
			throw new IllegalArgumentException();
	}

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		WebView webView = (WebView) findViewById(R.id.webView);
		if (Configuration.HOME_SHOW_WEBPAGE) {
			webView.getSettings().setJavaScriptEnabled(true);
			webView.loadUrl(Configuration.HOME_WEBPAGE_URL);
			webView.setWebViewClient(new WebViewClient() {
				public boolean shouldOverrideUrlLoading(WebView view, String url) {
					view.loadUrl(url);
					return true;
				}
			});
		} else {
			webView.setVisibility(View.GONE);
			// The background image is src/main/res/drawable-xxhdpi/background.jpg
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_upload) {
			DialogFragment newFragment = new SelectDialogFragment();
			newFragment.show(getSupportFragmentManager(), "SelectDialogFragment");
			return true;
		}
		if (id == R.id.action_settings) {
			startActivity(new Intent(this, SettingsActivity.class));
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
